<?php 

namespace app\branch\controller;
/**
 * 订单管理
 */
class Order extends \think\Controller
{
	use \app\common\traits\controller\TraitController;
	const ORDER_INITIAL_STAGE = 0;//未支付订单
    const ORDER_PAYMENT_STAGE = 1;//订单支付
    const ORDER_REFUND_STAGE  = 2;//订单退款
    const ORDER_COMPLETION    = 3;//订单完成
    const ORDER_CANCEL_STAGE  = 4;//订单取消
    const ORDER_CHECKIN_STAGE = 5;//入住订单
    const START_STATE         = 0;
    const STATE_END           = 1;
	//	'01'  => '等待支付',
	//  '10'  => '继续支付',
	//  '11'  => '已支付订单',
	//  '20'  => '订单退款中',
	//  '21'  => '已退款订单',
	//  '31'  => '订单完成',
	//  '41'  => '已取消订单'
	//	'50'  => '入住中'
	protected function init(){
		if (!model('Managers')->checkLogin()) {
            $this->redirect('@manager/login');
            exit;
        }
		$this->model = model('Orders');
		$this->list_route = url('/orders');
	}
	/**
	 * 待付款订单信息
	 */
	public function getIndexPay(){
		$query = input('get.');
		$camp_id = model('Campcontrols')->where('branch_id',model('Managers')->getLoginUserInfo('branch_id'))->column('id');
		$continuePayOrder = $this->model->getOrders(array_merge($query,['stage' => self::ORDER_INITIAL_STAGE, 'status' => self::STATE_END,'camp_id'=>['in',$camp_id]]));
		$waitPayOrder = $this->model->getOrders(array_merge($query,['stage' => self::ORDER_PAYMENT_STAGE, 'status' => self::START_STATE,'camp_id'=>['in',$camp_id]]));
	    $order_w_p = count($continuePayOrder) + count($waitPayOrder);
		return view('order/index_pay',['continuePayOrder'=>$continuePayOrder, 'order_w_p'=>$order_w_p, 'waitPayOrder' => $waitPayOrder]);
	}
	
	/**
	 * 待入住订单信息
	 */
	public function getIndexWaitIn(){
		$query = input('get.');
		$camp_id = model('Campcontrols')->where('branch_id',model('Managers')->getLoginUserInfo('branch_id'))->column('id');
		$order = $this->model->getOrders(array_merge($query,['stage' => self::ORDER_PAYMENT_STAGE, 'status' => self::STATE_END,'camp_id'=>['in',$camp_id]]));
		$order_d_z = count($order);
	    return view('order/index_wait_in',['order'=>$order,'order_d_z'=>$order_d_z]);
	}
	
	/**
	 * 入住中订单信息
	 */
	public function getIndexIn(){
	    $query = input('get.');
		$camp_id = model('Campcontrols')->where('branch_id',model('Managers')->getLoginUserInfo('branch_id'))->column('id');
		$order = $this->model->getOrders(array_merge($query,['stage' => self::ORDER_CHECKIN_STAGE, 'status' => self::START_STATE,'camp_id'=>['in',$camp_id]]));
		$order_r_z = count($order);
	    return view('order/index_in',['order'=>$order,'order_r_z'=>$order_r_z]);
	}
	
	/**
	 * 已完成订单信息
	 */
	public function getIndexComplete(){
		$query = input('get.');
		$camp_id = model('Campcontrols')->where('branch_id',model('Managers')->getLoginUserInfo('branch_id'))->column('id');
		$order = $this->model->getOrders(array_merge($query,['stage' => self::ORDER_COMPLETION, 'status' => self::STATE_END,'camp_id'=>['in',$camp_id]]));
		$order_w_c = count($order);
	    return view('order/index_complete',['order'=>$order,'order_w_c'=>$order_w_c]);
	}
	
	/**
	 * 退款中的订单信息.........................................
	 */
	public function getRefundOn(){
		$query = input('get.');
		$camp_id = model('Campcontrols')->where('branch_id',model('Managers')->getLoginUserInfo('branch_id'))->column('id');
		$order = $this->model->getOrders(array_merge($query,['stage' => self::ORDER_REFUND_STAGE, 'status' => self::START_STATE,'camp_id'=>['in',$camp_id]]));
		$order_t_k = count($order);
	    return view('order/refund_on',['order'=>$order,'order_t_k'=>$order_t_k]);
	}
	/**
	 * 已取消的订单
	 */
	public function getIndexCancel(){
		$query = input('get.');
		$camp_id = model('Campcontrols')->where('branch_id',model('Managers')->getLoginUserInfo('branch_id'))->column('id');
		$order = $this->model->getOrders(array_merge($query,['stage' => self::ORDER_CANCEL_STAGE, 'status' => self::STATE_END,'camp_id'=>['in',$camp_id]]));
	    $order_num = count($order);
	    return view('order/index_cancel',['order'=>$order,'order_num'=>$order_num]);
	}
	// /**
	//  * 已离开订单信息
	//  */
	// public function getLeaved(){
	// 	$query = input('get.');
	// 	$order = $this->model->getOrders(array_merge($query,['status' => self::ORDER_LEAVE_STATUS]));
	// 	$order_y_k = count($order);
	//     return view('order/leaved',['order'=>$order,'order_y_k'=>$order_y_k]);
	// }
	
	// /**
	//  * 返还押金中订单信息
	//  */
	// public function getRefundDeposit(){
	// 	$query = input('get.');
	// 	$order = $this->model->getOrders(array_merge($query,['status' => self::ORDER_REFUND_DEPOSIT_STATUS]));
	// 	$order_f_y = count($order);
	//     return view('order/refund_deposit',['order'=>$order,'order_f_y'=>$order_f_y]);
	// }
	
	/**
	 * 已退款订单
	 */
	public function getRefunded(){
		$query = input('get.');
		$camp_id = model('Campcontrols')->where('branch_id',model('Managers')->getLoginUserInfo('branch_id'))->column('id');
		$order = $this->model->getOrders(array_merge($query,['stage' => self::ORDER_REFUND_STAGE, 'status' => self::STATE_END,'camp_id'=>['in',$camp_id]]));
	    $order_num = count($order);
	    return view('order/refunded',['order'=>$order,'order_num'=>$order_num]);
	}

	/**
	 * 订单详情
	 */
	public function getRead(){
		$order = $this->model->getOrderInfo(input('get.id')); 
		$leave_car_date=strtotime($order['leave_car_date']);
        $take_car_date=strtotime($order['take_car_date']);
        $date=($leave_car_date-$take_car_date)/86400;
	    return view('order/read',['order' => $order,'date'=>$date]);
	} 
}






 ?>